package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import com.itheima.health.vo.MenuVO;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    //动态菜单 dzf
    @Reference
    private MenuService menuService;

    @Override
    public UserVO findUserDetailsByUsername(String username) {
        //1.根据用户名称查询用户表,得到用户的ID
        UserVO userVO = baseMapper.findUserIdByUsername(username);
        //2.根据用户的ID,通过中间表,可以查询得到用户的角色
        Integer uid = userVO.getId();
        List<RoleVO> roleVOSet = baseMapper.findUserRolesByUserId(uid);
        //3.根据用户的角色ID,通过中间表可以查询得到用户的权限
        for (RoleVO roleVO : roleVOSet) {
            List<Permission> permissionSet = baseMapper.findUserPermissionByRoleId(roleVO.getId());
            roleVO.setPermissionList(permissionSet);
        }
        userVO.setRoles(roleVOSet);
        return userVO;
    }

    /* 动态展示菜单
      todoby 董子帆
      */
    @Override
    public List<MenuVO> getMenu(String username) {
        List<MenuVO> retList = new ArrayList<>();
        UserVO info = baseMapper.findUserInfoByUsername(username);
        List<RoleVO> roles = baseMapper.findRolesByUid(info.getId());
        List<Integer> list = new ArrayList<>();
        for (RoleVO role : roles) {
            List<Integer> menuIdByRoleId = baseMapper.findMenuIdByRoleId(role.getId());
            list.addAll(menuIdByRoleId);
        }

        for (Integer id : list) {

            List<MenuVO> menuList = menuService.getMenuList(id);
            retList.addAll(menuList);
        }
        return change(retList);
    }
    private List<MenuVO> change(List<MenuVO> retList) {
        ArrayList<MenuVO> result = new ArrayList<>();
        ArrayList<MenuVO> parent = new ArrayList<>();
        ArrayList<MenuVO> child = new ArrayList<>();
        for (MenuVO menuVO : retList) {
            if(menuVO.getPath().startsWith("/"))
                child.add(menuVO);
            else
                parent.add(menuVO);

        }

        for (MenuVO menuVO : parent) {
            ArrayList<MenuVO> list = new ArrayList<>();
            String path = menuVO.getPath();
            for (MenuVO vo : child) {
                if(vo.getPath().startsWith("/"+path)){
                    list.add(vo);
                    //child.remove(vo);
                }
            }
            menuVO.setChildren(list);
            result.add(menuVO);
        }
        return result;
    }
}
